﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using Universum.Infrastructure.Utils.Validation;

using System.Text;
using Universum.Infrastructure.Utils.Extensions;
using StructureMap;
using Universum.Model.Services;
namespace Universum
{
	/// <summary>
	/// Extensions for the String class
	/// </summary>
	public static class StringExtensions
	{
		public static string AsSlug(this string @string)
		{
			return ObjectFactory.GetInstance<IFriendlyUrlService>().CreateSlugFrom(@string);
		}
		public static string TrimFront(this string @string, char @char)
		{
			StringBuilder sb = new StringBuilder(@string);
			int index = 0;

			while (sb[index] == @char && sb.Length > index)
			{
				sb.Remove(index, 1);
			}
			return sb.ToString();
		}
		public static string TrimBack(this string @string, char @char)
		{
			StringBuilder sb = new StringBuilder(@string);
			int index = sb.Length-1;

			while (sb[index] == @char)
			{
				sb.Remove(index, 1);
				index = sb.Length - 1;
			}
			return sb.ToString();
		}
		public static bool IsValidEmail(this string s)
		{
			return new RegularExpressions().GetExpression(RegularExpressions.IS_EMAIL).Match(s).Success;
		}

		public static bool IsValidUsername(this string input)
		{
			return new RegularExpressions().GetExpression(RegularExpressions.USERNAME).Match(input).Success;
		}

		public static bool IsNullOrEmpty(this string s)
		{
			return string.IsNullOrEmpty(s);
		}
		private static readonly Regex cleanWhitespace = new Regex(@"\s+", RegexOptions.Compiled | RegexOptions.Singleline);

		public static string IsRequired(this string s)
		{
			if (string.IsNullOrEmpty(s))
			{
				throw new ValidationException(string.Format("String is required: {0}", s));
			}

			return s;
		}

		public static string CleanHtmlTags(this string s)
		{
			Regex exp = new Regex(
				"<[^<>]*>",
				RegexOptions.Compiled
				);

			return exp.Replace(s, "");
		}
			

		public static string CleanWhitespace(this string s)
		{
			return cleanWhitespace.Replace(s, " ");
		}


		private static readonly Regex uriEncodedRegex = new Regex("(^|[^\\w'\"]|\\G)(?<uri>(?:https?|ftp)&#58;&#47;&#47;(?:[^./\\s'\"<)\\]]+\\.)+[^./\\s'\"<)\\]]+(?:&#47;.*?)?)(\\.[\\s<'\"]|[\\s,<'\"]|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
		

		public static string Shorten(this string s, int characterCount)
		{
			string text = !string.IsNullOrEmpty(s) ? s.CleanHtmlTags().CleanWhitespace() : "";

			if (!string.IsNullOrEmpty(text) && characterCount > 0 && text.Length > characterCount)
			{
				text = text.Substring(0, characterCount);
			}

			return text;
		}

		public static bool GuidTryParse(this string s, out Guid result)
		{
			if (s == null)
			{
				throw new ArgumentNullException("s");
			}

			try
			{
				result = new Guid(s);
				return true;
			}
			catch (FormatException)
			{
				result = Guid.Empty;
				return false;
			}
			catch (OverflowException)
			{
				result = Guid.Empty;
				return false;
			}
		}

		public static string ComputeHash(this string value)
		{
			string hash = value;

			if (value != null)
			{
				MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
				byte[] data = Encoding.ASCII.GetBytes(value);
				data = md5.ComputeHash(data);
				hash = "";
				for (int i = 0; i < data.Length; i++)
				{
					hash += data[i].ToString("x2");
				}
			}

			return hash;
		}
	}

}
